//
//  UIBarButton+Extension.swift
//  Microblog
//
//  Created by Roman on 17/1/4.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit

//给UIBarButtonItem扩充构造方法, 或者类工厂方法

extension UIBarButtonItem {
    //扩展一个快速创建包含自定义按钮的UIBarButtonItem对象
    convenience init(title: String = "", imageName: String? = nil, target: Any?, action: Selector?) {
        
        //创建Btn,添加图片
        let btn = UIButton()
        if let img = imageName {
            
            btn.setImage(UIImage(named:img ), for: .normal)
            btn.setImage(UIImage(named:img + "_highlighted"), for: .highlighted)
        }
        btn.setTitle(title, for: .normal)
        //设置文字颜色
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        //字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //添加点击事件
        if let ac = action {
            btn.addTarget(target, action: ac, for: UIControlEvents.touchUpInside)
        }
        //需要设置自适应大小
        btn.sizeToFit()
        //使用self调用指定的够着函数创建对象
        self.init()
        self.customView = btn
    }
    
}
